package cn.rengy.web.framework.sys.menu.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.rengy.web.framework.sql.DbService;
import cn.rengy.web.framework.util.GetSessionUtils;

@Service
public class MenuService {
	@Autowired
	private DbService dbService;
	private static Logger logger = LoggerFactory.getLogger(MenuService.class);
	
	/**
	 * 保存新增菜单
	 * @param paramMap
	 */
	@Transactional
	public void update(Map<String,Object> paramMap){
		Object id=paramMap.get("id");
		if(id==null||StringUtils.isBlank(id.toString())){
			long insertid=dbService.updateReturnGeneratedKeys("insert_sys_menu", paramMap).longValue();
			//菜单授权
			Long userid= GetSessionUtils.getUserId();
			Map<String,Object> paramMap2=new HashMap<String,Object>();
			paramMap2.put("id", userid);
			paramMap2.put("target_id", insertid);
			paramMap2.put("role_rel_definition_id", 1);
			this.dbService.updateMapParameter("insert_role_menu_rel_byulid", paramMap2);
		}else{
			dbService.updateMapParameter("update_sys_menu", paramMap);
		}
	}
	/**
	 * 删除菜单
	 * @param menuid
	 */
	@Transactional
	public void deleteMenu(String menuid){
		this.dbService.update("delete_role_rel", 1,menuid);
		this.dbService.update("delete_sys_menu", menuid);
	}
	public List<Map<String,Object>> query(){
		Long user_id=GetSessionUtils.getUserId();
		return this.dbService.queryForList("select_menu_user",user_id);
		
	}
}
